

<!DOCTYPE html>
<html lang="null">
<head prefix="og: http://ogp.me/ns#">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <meta name="description" content="保存一下自己积累的技术资料">
    <title>list.sort 方法和内置sorted | 黄龙天空</title>

    <!-- Open Graph -->
    <meta name="description" content="&amp;#x4E0E;&amp;#x5B57;&amp;#x9762;&amp;#x610F;&amp;#x601D;&amp;#x4E00;&amp;#x6837;&amp;#xFF0C;&amp;#x8FD9;&amp;#x4E24;&amp;#x4E2A;&amp;#x65B9;&amp;#x6CD5;&amp;#x90FD;&amp;#x662F;&amp;#x7528;&amp;#x4E8E;&amp;#x6392;&amp;#x5E8F;&amp;#x7684;&amp;#xFF0C;&amp;#x4E0D;&amp;#x540C;&amp;#x7684;&amp;#x662F;">
<meta property="og:type" content="article">
<meta property="og:title" content="list.sort 方法和内置sorted">
<meta property="og:url" content="http://huanglongtiankong.oschina.io/2017/05/17/sorted/index.html">
<meta property="og:site_name" content="黄龙天空">
<meta property="og:description" content="&amp;#x4E0E;&amp;#x5B57;&amp;#x9762;&amp;#x610F;&amp;#x601D;&amp;#x4E00;&amp;#x6837;&amp;#xFF0C;&amp;#x8FD9;&amp;#x4E24;&amp;#x4E2A;&amp;#x65B9;&amp;#x6CD5;&amp;#x90FD;&amp;#x662F;&amp;#x7528;&amp;#x4E8E;&amp;#x6392;&amp;#x5E8F;&amp;#x7684;&amp;#xFF0C;&amp;#x4E0D;&amp;#x540C;&amp;#x7684;&amp;#x662F;">
<meta property="og:updated_time" content="2017-06-16T09:35:18.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="list.sort 方法和内置sorted">
<meta name="twitter:description" content="&amp;#x4E0E;&amp;#x5B57;&amp;#x9762;&amp;#x610F;&amp;#x601D;&amp;#x4E00;&amp;#x6837;&amp;#xFF0C;&amp;#x8FD9;&amp;#x4E24;&amp;#x4E2A;&amp;#x65B9;&amp;#x6CD5;&amp;#x90FD;&amp;#x662F;&amp;#x7528;&amp;#x4E8E;&amp;#x6392;&amp;#x5E8F;&amp;#x7684;&amp;#xFF0C;&amp;#x4E0D;&amp;#x540C;&amp;#x7684;&amp;#x662F;">

    <script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Person",
  "email": "mailto:1300896173@qq.com",
  "image": "undefined",
  "name": "黄龙天空",
  "url": "http://huanglongtiankong.oschina.io"
}
</script>
    <script>
    var algoliaEnabled = false;
    
</script>

    

    
    
        <link href="//fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
    
    
    
        <!-- stylesheets list from config.yml -->
        
        <link rel="stylesheet" href="/css/aloha.css">
        
        <link rel="stylesheet" href="//cdn.bootcss.com/semantic-ui/2.2.4/semantic.min.css">
        
        <link rel="stylesheet" href="//cdn.bootcss.com/magnific-popup.js/1.1.0/magnific-popup.min.css">
        
    

</head>
<body id="body" style="margin-bottom: 0;" class="pushable">

    <div class="ui top fixed menu">
        <a class="item" id="menu-icon"><i class="sidebar icon"></i></a>
    </div>

    <div id="menu-sidebar" class="ui left vertical sidebar menu">

    <div id="sidebar-top">
        <div class="content">
            <h3>黄龙天空</h3>
        </div>
    </div>
    <div class="ui container sidebar-card">
    <div class="ui people shape content">
        <div class="active side">
            <div class="ui card">
                <div class="image">
                    <img class="ui medium bordered image" src="/images/avatar.jpg">
                </div>
                <div class="content">
                    <a class="header">黄龙天空</a>
                    
                </div>
                <div class="extra content">
                    <div class="ui list">
                        
                        
                        <div class="item">
                            <i class="mail icon" style="float: left"></i>
                            <div class="content">
                                <a href="mailto:1300896173@qq.com">1300896173@qq.com</a>
                            </div>
                        </div>
                        
                    </div>

                </div>
                <div class="extra content">
                    <div class="ui list">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>


</div>

    
    

    

    
    <a href="/index.html" class="item" >
        <i class="home icon"></i>
        HOME
    </a>

    

    
</div>

    <div class="pusher body-content">
        <div id="content" class="ui main container">
            <!--<div class="ui one column grid">-->
                <!--<div class="column">-->
                    <!--<div class="ui main container">-->
                        <div id="post-sorted" class="ui main container article-type-post">

    

    <div class="ui divided grid">

        <div class="two column row" id="article-content">

            <div class="thirteen wide computer thirteen wide tablet sixteen wide mobile column">
                

<h1 class="ui header">
    
    list.sort 方法和内置sorted
</h1>



                <div class="article-inner">

                    <div class="article-entry" itemprop="articleBody">
                        
                        <p>&#x4E0E;&#x5B57;&#x9762;&#x610F;&#x601D;&#x4E00;&#x6837;&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x7528;&#x4E8E;&#x6392;&#x5E8F;&#x7684;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x662F;list.sort&#x65B9;&#x6CD5;&#x4F1A;&#x5C31;&#x5730;&#x6392;&#x5E8F;&#x5217;&#x8868;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x503C;&#x662F;None&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4E0D;&#x4F1A;&#x7ED9;&#x4F60;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x5217;&#x8868;&#xFF0C;&#x800C;&#x662F;&#x628A;&#x539F;&#x6709;&#x7684;list&#x8FDB;&#x884C;&#x4E86;&#x6392;&#x5E8F;&#x3002;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>&gt;&gt;&gt; s = [1,5,2,6,7,3,4]
&gt;&gt;&gt; s.sort()
&gt;&gt;&gt; s
[1, 2, 3, 4, 5, 6, 7]
</code></pre><p>sorted&#x5219;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5217;&#x8868;&#x4F5C;&#x4E3A;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4EFB;&#x4F55;&#x7684;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0D;&#x8FC7;&#x6700;&#x540E;&#x8FD4;&#x56DE;&#x7684;&#x90FD;&#x4F1A;&#x662F;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x3002;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>&gt;&gt;&gt; s = (1,5,2,6,7,3,4)
&gt;&gt;&gt; sorted(s)
[1, 2, 3, 4, 5, 6, 7]
</code></pre><p>&#x4E0D;&#x7BA1;&#x662F;list.sort&#x65B9;&#x6CD5;&#x8FD8;&#x662F;sorted&#x51FD;&#x6570;&#xFF0C;&#x90FD;&#x6709;&#x4E24;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x3002;<br>reverse: &#x5982;&#x679C;&#x88AB;&#x8BBE;&#x5B9A;&#x4E3A;True,&#x5143;&#x7D20;&#x4F1A;&#x5012;&#x5E8F;&#x8F93;&#x51FA;&#x3002;&#x9ED8;&#x8BA4;&#x503C;&#x662F;False<br>key: &#x4E00;&#x4E2A;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4F1A;&#x628A;&#x5E8F;&#x5217;&#x91CC;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x5C06;&#x662F;&#x6392;&#x5E8F;&#x7B97;&#x6CD5;&#x4F9D;&#x8D56;&#x7684;&#x5BF9;&#x6BD4;&#x5173;&#x952E;&#x5B57;&#x3002; &#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>&gt;&gt;&gt; s = [-5,4,1,-3,2]
&gt;&gt;&gt; s.sort(reverse=True,key=abs)
&gt;&gt;&gt; s
[-5, 4, -3, 2, 1]
&gt;&gt;&gt; sorted(s, reverse=True, key=abs)
[-5, 4, -3, 2, 1]
# abs&#x662F;&#x53D6;python&#x4E2D;&#x53D6;&#x7EDD;&#x5BF9;&#x503C;&#x7684;&#x51FD;&#x6570;
</code></pre>
                        
                    </div>

                    
                    
                    

                    <div class="description post-description">
    <span class="post-description-item">
        <time datetime="2017-05-17T06:03:10.000Z" itemprop="datePublished">2017-05-17</time>

    </span>
    <span class="post-description-item">
        

    <span>
        <i class="tags icon"></i>
    </span>

    
    <span class="ui tiny label">
        <a href="/tags/python/">python</a>
    </span>

    

    </span>
</div>

                </div>
                
                <div id="pagination" class="nav-web ui text container pagination">

    <div class="ui stackable two column divided grid container">
        <div class="row">
            <div class="column nav-left">
                

                <a href="/2017/06/08/高并发接口的一些坑/" class="ui tiny button">

                    <i class="angle left icon"></i>
                    
                    记录一些写高并发接口过程中遇到的坑
                    

                </a>

                
            </div>
            <div class="column nav-right">
                

                <a href="/2017/03/04/linux命令/" class="ui tiny button">
                    
                    linux命令
                    
                    <i class="angle right icon"></i></a>
                
            </div>
        </div>
    </div>

</div>
                
            </div>

            
            <div class="three wide computer three wide tablet column">

                <div class="ui sticky article-toc" id="article-toc">
                    <!-- toc -->
                    
                </div>

            </div>
            

        </div>
    </div>
</div>


                    <!--</div>-->
                <!--</div>-->
            <!--</div>-->
        </div>
        <div class="ui vertical footer segment">
    <div class="ui center aligned container">
        <div class="ui inverted section divider"></div>
        &copy; 2017 <a href="/">黄龙天空</a>,
        Powered by <a href="https://github.com/henryhuang/hexo-theme-aloha" target="_blank">Aloha</a> and <a
                href="http://hexo.io/" target="_blank">Hexo</a>.
    </div>
</div>

    </div>




<!-- scripts list from theme config.yml -->

<script src="//cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>

<script src="//cdn.bootcss.com/semantic-ui/2.2.4/semantic.min.js"></script>

<script src="//cdn.bootcss.com/algoliasearch/3.18.1/algoliasearch.min.js"></script>

<script src="//cdn.bootcss.com/algoliasearch-helper-js/2.13.0/algoliasearch.helper.min.js"></script>

<script src="/js/semantic-ui-algolia.js"></script>

<script src="/js/aloha-events.js"></script>



</body>
</html>
